<?php
# The header controller

include_once("template.inc");
require_once("scripts/global_2.php");
require_once("scripts/constraint.php");
require_once("scripts/EKDocument.php");
require_once("scripts/EKParagraphRelationship.php");
require_once("scripts/EKTalk.php");
require_once("scripts/EKUser.php");
require_once("scripts/EKEvaluation.php");
require_once("scripts/EKStatistics.php");
require_once("scripts/EKEvent.php");
session_start();

function setTemplateWithSession($template)
{
	if(!isset($_SESSION["position"])){
		$_SESSION["position"] = "document.php?id=_0&method=v";
	}
	if(!isset($_SESSION['userid'])||$_SESSION['userid']==""){
	# If not login
		$template->set_file("session","template/user_login.html");
	} else {
	# Login
		$template->set_file("session","template/user_status.html");
		
		$currentUser = new EKUser($_SESSION['userid']);
		$_SESSION["username"] = $currentUser->getName();
		$_SESSION["userType"] = $currentUser->getType();
		
		$template->set_var("user_id",$_SESSION['userid']);
		$template->set_var("username",$_SESSION['username']);
		$myArray = Array( 0 => "User",
						1 => "Committee",
						2 => "Arbitrator",
						3 => "Administrator");
		$template->set_var("user_type",	$myArray[$_SESSION['userType']]);
		$template->set_var("user_lv"," Lv: <strong>".$currentUser->getLevel()."</strong>");
		$template->set_var("exp",$currentUser->getExp());
			
		$template->set_var("user_rating_a_value",$currentUser->getRatingA());
		$template->set_var("user_rating_b_value",$currentUser->getRatingB());
		$template->set_var("email",$currentUser->getEmail());
		$template->set_var("selfIntro",$currentUser->getSelfIntro());
	}
	$template->parse("user_session","session");
}
function getEvents($template){
	$template->set_file("events","template/events.html");
	
	$template->set_block("events","event_list","eList");
	$template->set_var("eList");
	
	$eventArray = EKEvent::getNewEvents();
	$typeConvert = Array(
						"r" => "Rating",
						"d" => "Document",
						"p" => "Paragraph",
						"t" => "Talk"
					);
	
	foreach($eventArray as $event){
		$template->set_var("element_type", $typeConvert[$event[2]]);
		if($event[2] == "t"){
			$template->set_var("element_predicate", "to");
		} else if($event[2] == "p") {
			$template->set_var("element_predicate", "of");
		} else {
			$template->set_var("element_predicate", "");
		}
		
		if(EKDocument::testExist($event[6])){
			$template->set_var("element_link", "document.php?id=".$event[6]."&method=v");
		}else{
			$template->set_var("element_link", "#");
		}
		$template->set_var("element_name", $event[7]);
		
		$template->set_var("event_type", $event[4]);
		$template->set_var("user_name",EKUser::getAttributeById($event[1],"name"));
		$template->set_var("user_link","user.php?userid=".$event[1]."&method=v");
		$template->set_var("update_time", date("Y/m/d [H:i:s]",$event[5]));
		$template->parse("eList","event_list",true);
	}
	
	$template->parse("new_events","events");
}
?>